﻿
using System;
using System.Data;
using System.Collections.Generic;

namespace LDSP.BRL
{
    public partial class Plan : BYFW.BOL.BaseEntity
    {
        public Plan()
        {
            m_tableName = "t_plan";
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
        }

        protected string m_person = string.Empty;
        protected DateTime m_expiredDT = DateTime.Now;
        protected int m_favoriteID = 0;
        protected int m_status = 0;
        protected int m_type = 0;
        protected string m_department = string.Empty;
        protected string m_progress = string.Empty;
        protected string m_email = string.Empty;
        protected DateTime m_changeDT = DateTime.Now;

        protected R_EnterpriseCodification m_enterpriseCodification = null;

        /// <summary>
        /// 相关人员
        /// </summary>
        public string Person
        {
            get { return m_person; }
            set { m_person = value; }
        }
        /// <summary>
        /// 到期时间
        /// </summary>
        public DateTime ExpiredDT
        {
            get { return m_expiredDT; }
            set { m_expiredDT = value; }
        }
        /// <summary>
        /// 收藏关系ID
        /// </summary>
        public int FavoriteID
        {
            get { return m_favoriteID; }
            set { m_favoriteID = value; }
        }
        /// <summary>
        /// 状态
        /// </summary>
        public int Status
        {
            get { return m_status; }
            set { m_status = value; }
        }
        /// <summary>
        /// 类型,0是自定义；1是指派
        /// </summary>
        public int Type
        {
            get { return m_type; }
            set { m_type = value; }
        }
        /// <summary>
        /// 部门/人员
        /// </summary>
        public string Department
        {
            get { return m_department; }
            set { m_department = value; }
        }
        /// <summary>
        /// 项目进度概述
        /// </summary>
        public string Progress
        {
            get { return m_progress; }
            set { m_progress = value; }
        }
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime ChangeDT
        {
            get { return m_changeDT; }
            set { m_changeDT = value; }
        }
        /// <summary>
        /// 项目其它成员email,以分号隔开
        /// </summary>
        public string Email
        {
            get { return m_email; }
            set { m_email = value; }
        }
        public R_EnterpriseCodification EnterpriseCodification
        {
            get
            {
                if (m_enterpriseCodification == null)
                {
                    m_enterpriseCodification = new R_EnterpriseCodification();
                    if (m_favoriteID != 0)
                    {
                        m_enterpriseCodification.GetProperties(m_favoriteID);
                    }
                }
                return m_enterpriseCodification;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("person", m_person);
            m_container.Add("expiredDT", m_expiredDT);
            m_container.Add("favoriteID", m_favoriteID);
            m_container.Add("status", m_status);
            m_container.Add("department", m_department);
            m_container.Add("progress", m_progress);
            m_container.Add("changeDT", m_changeDT);
            m_container.Add("email", m_email);
            m_container.Add("type", m_type);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_person = this.GetStringValue(record, "person");
            m_expiredDT = this.GetDateValue(record, "expiredDT");
            m_favoriteID = this.GetIntValue(record, "favoriteID");
            m_status = this.GetIntValue(record, "status");
            m_department = this.GetStringValue(record, "department");
            m_progress = this.GetStringValue(record, "progress");
            m_changeDT = this.GetDateValue(record, "changeDT");
            m_email = this.GetStringValue(record, "email");
            m_type = this.GetIntValue(record, "type");
            return true;
        }

        public void DeleteByCodification(string transID, int favoriteID)
        {
            string cond = string.Format("where {0}={1}", DB.GetCol("favoriteID"), DB.GetVal(favoriteID));
            this.DeleteRecord(transID, cond);
        }

        public DataTable GetRecordsByEnterprise(int enterpriseID, string cond)
        {
            string select = @"select * from (select t_plan.*, t2.name as docName from t_plan, t_r_enterprise_codification t1, t_codification t2 where t_plan.favoriteID = t1.id and t1.enterpriseID = {0} and t1.docID=t2.id) t";
            string sql = string.Empty;

            if (cond.Length > 0)
            {
                sql = string.Format(select + " {1} ", enterpriseID, cond);
            }
            else
            {
                sql = string.Format(select, enterpriseID);
            }

            return this.DB.Select(sql).Tables[0];
        }

        public DataTable GetRecordsByEnterpriseAndUser(int enterpriseID, int userID, string cond)
        {
            string select = @"select * from (select t_plan.* from t_plan inner join t_r_enterprise_codification t1 on t_plan.favoriteID = t1.id inner join t_r_plan_user t2 on t_plan.id = t2.planID where t1.enterpriseID = {0} and t2.userID = {1}) t";
            string sql = string.Empty;

            if (cond.Length > 0)
            {
                sql = string.Format(select + " {2} ", enterpriseID, userID, cond);
            }
            else
            {
                sql = string.Format(select, enterpriseID, userID);
            }

            return this.DB.Select(sql).Tables[0];
        }

        public List<int> GetEnterpriseUserIDsByPlan()
        {
            List<int> users = new List<int>();

            return users;
        }

        public List<int> GetEnterpriseUsersByPlan(int ownerID)
        {
            List<int> ids = new List<int>();
            List<R_PlanPrinciple> planPrincipleList = this.GetSpecialList<R_PlanPrinciple>(null, "planID", ownerID);
            foreach (R_PlanPrinciple r in planPrincipleList)
            {
                ids.Add(r.UserID);
            }

            return ids;
        }

        public List<Plan> GetReportForStatus(int enterpriseID, string cond)
        {
            string sql = string.Format(@"
select p.* from t_codification t, t_r_enterprise_codification t1, t_plan p 
where p.FavoriteID=t1.id 
and t.id=t1.docID 
and {0}={1} {2}",
    "t1.enterpriseID", DB.GetVal(enterpriseID), cond);
            System.Collections.Generic.List<Plan> res = new System.Collections.Generic.List<Plan>();
            DataTable dt = DB.Select(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Plan ett = new Plan();
                ett.GetProperties(dt.Rows[i]);
                res.Add(ett);
            }
            return res;
        }

        public DataTable GetEGReportForStatus(int enterpriseID, string cond)
        {
            string sql = string.Format(@"
select e.code as name, p.status as priority,count(p.status) as number 
from t_codification t, t_r_enterprise_codification t1, t_plan p, t_enterprise e
where p.FavoriteID=t1.id 
and t.id=t1.docID 
and t1.enterpriseID = e.id
and t1.enterpriseID in (select id  from t_enterprise where parentID={0}) 
group by e.code,p.status
order by e.code
{1}",
DB.GetVal(enterpriseID), cond);

            return DB.Select(sql).Tables[0];
        }

        /// <summary>
        /// 根据状态获取任务计划
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<Plan> GetPlansByStatus(int status)
        {
            return this.GetSpecialList<Plan>(null, " where status = " + status);
        }

        /// <summary>
        /// Copy a new plan.
        /// </summary>
        /// <returns></returns>
        public Plan Copy()
        {
            Plan plan = new Plan();

            plan.Comment = Comment;
            plan.Department = Department;
            plan.ExpiredDT = ExpiredDT;
            plan.FavoriteID = FavoriteID;
            plan.Name = Name;
            plan.Person = Person;
            plan.Progress = Progress;
            plan.Status = Status;
            plan.Type = Type;


            return plan;
        }
    }
}

